package com.apestech.framework.stream;

import java.sql.SQLException;
import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.apestech.formwork.stream.ListStream;
import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.util.callback.SpiltStrCallback;

public class ArrayObject extends StringUtil {
	
	private Object obj;
	private int index=-1;
	private int rowCount=-1;
	
	public ArrayObject(Object obj) throws Exception{
		if(obj instanceof String){
			ArrayList<String> list=new ArrayList<String>();
			execSplitStr(castToString(obj), ";", new SpiltStrCallback() {
				@Override
				public void exec(String id) throws Exception {
					list.add(id);
				}
			});
			this.obj=list;
		} else {
			this.obj=obj;
		}
		getRowCount();
	}
	
	public int size(){
		return this.rowCount;
	}

	private void getRowCount() throws Exception{
		if(this.obj==null){
			this.rowCount=0;
			return;
		}
		if(this.obj instanceof JSONArray){
			this.rowCount=((JSONArray) obj).size();
		} else if(this.obj instanceof ArrayList<?>){
			this.rowCount=((ArrayList<?>) obj).size();
		} else if(this.obj instanceof ListStream){
			this.rowCount=((ListStream) obj).size();
		} else if(this.obj instanceof String[]){
			this.rowCount=((String[]) obj).length;
		} else if(this.obj instanceof String[][]){
			this.rowCount=((String[][]) obj).length;
		}
	}
	
	public boolean next() throws Exception{
		this.index++;
		if(this.obj instanceof SqlResultSet){
			return ((SqlResultSet) this.obj).next(); 
		} else if(this.obj instanceof ListStream){
			return ((ListStream) this.obj).next();
		} else {
			return this.index<this.rowCount;
		}
	}
	
	public int getIndex(){
		return this.index;
	}
	
	public String getText() throws Exception{
		return getText(this.index);
	}

	public String getText(int i) throws Exception{
		String text="";
		if(this.obj instanceof SqlResultSet){
			if(i==index){
				text=JSON.toJSONString(((SqlResultSet)this.obj).toRowJson());
			}
		} else if(this.obj instanceof JSONArray){
			text=JSON.toJSONString(((JSONArray)this.obj).get(i));
		} else if(this.obj instanceof ListStream){
			text=((ListStream)this.obj).getText(i);
		} else if(this.obj instanceof ArrayList<?>){
			text=((ArrayList<?>)this.obj).get(i).toString();
		} else if(this.obj instanceof String[]){
			text=((String[])this.obj)[i];
		} else if(this.obj instanceof String[][]){
			text=((String[][])this.obj)[i].toString();
		}
		return text;
	}

	public Object getObject(int i) throws Exception{
		Object result=null;
		if(this.obj instanceof SqlResultSet){
			if(i==index){
				result=((SqlResultSet)this.obj).toRowJson();
			}
		} else if(this.obj instanceof JSONArray){
			result=((JSONArray)this.obj).get(i);
		} else if(this.obj instanceof ListStream){
			result=((ListStream)this.obj).getText(i);
		} else if(this.obj instanceof ArrayList<?>){
			result=((ArrayList<?>)this.obj).get(i);
		} else if(this.obj instanceof String[]){
			result=((String[])this.obj)[i];
		} else if(this.obj instanceof String[][]){
			result=((String[][])this.obj)[i].toString();
		}
		return result;
	}


	
	public JSONObject getJSON() throws Exception{
		if(this.obj instanceof SqlResultSet){
			return ((SqlResultSet)this.obj).toRowJson();
		}
		return null;
	}
	
	public Object getObj(){
		return this.obj;
	}
	
	public String toString(){
		String text="";
		if(this.obj instanceof JSONArray){
			text=((JSONArray)this.obj).toString();
		} else if(this.obj instanceof ArrayList<?>){
			text=((ArrayList<?>)this.obj).toString();
		} else if(this.obj instanceof String[]){
			text=((String[])this.obj).toString();
		} else if(this.obj instanceof String[][]){
			text=((String[][])this.obj).toString();
		}
		return text;
	}
	
	public void close() throws SQLException{
	}

}
